package xuecheng.tool.logservice.alibaba;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import com.aliyun.openservices.log.Client;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * 自定义阿里日志服务配置
 *
 * @author PengXueCheng
 * @version 1.0.0
 */
@SpringBootConfiguration
@EnableConfigurationProperties(LogProperties.class)
@ConditionalOnProperty(prefix = "cpx.log", value = "enable", havingValue = "true")
@Slf4j
public class CpxLogAutoConfig {

    @Autowired
    private LogProperties logProperties;

    @Bean
    public Client client() {
        // 构建一个客户端实例
        @NonNull
        String endpoint = logProperties.getEndpoint();
        @NonNull
        String accessKey = logProperties.getAccessKey();
        @NonNull
        String secretKey = logProperties.getSecretKey();
        Client client = new Client(endpoint, accessKey, secretKey);
        log.info("阿里日志服务客户端启动成功!!! project = {}, logstores = {}, endpoint = {}", logProperties.getProject(),
            logProperties.getLogstores(), endpoint);
        return client;
    }

    @Bean
    public AliLogUtils aliLogUtils(Client client) {
        return new AliLogUtils(client);
    }

}
